package com.bootdo.common.utils.encryptionUtil;


import java.io.*;
import com.bootdo.common.utils.encryptionUtil.Seal.bytearray;

import static com.bootdo.common.utils.encryptionUtil.Header.getHeader;
import static com.bootdo.common.utils.encryptionUtil.Seal.BUFFER_SIZE;
import static com.bootdo.common.utils.encryptionUtil.Seal.InitEncryptKey;

public class FileEncryption {
    // private static Logger logger = Logger.getLogger(FileEncryption.class);
    static int ENCFILE_HEAD_LEN = 1024;

    /**
     * 加密文件的操作，返回值为int类型，根据返回值的不同，接收不同的结果
     * 1：文件正常解密
     * -1：文件没有找到
     * -2：在写入文件时出现的其他异常
     *
     * @param originalFile
     * @param targetFile
     * @throws Exception
     */
    public static int EncryptFile(File originalFile, File targetFile) throws Exception {
	    /*File originalFile=new File(originalFile);
	    File targetFile=new File(targetFile);*/
        Seal seal = new Seal();
        Seal.seal_ctx sc = seal.new seal_ctx();
        InitEncryptKey(seal, sc);
        PCIPHER_HEADER pHeader = new Header().BuildCipherHeader();

        String source = "";
        String destination = "";
        try {
            source = originalFile.getPath();
            destination = targetFile.getPath();
        } catch (Exception e) {
            // logger.error("文件 "+originalFile+" 没有找到");
            // logger.error("文件 "+originalFile+"加密后的文件"+targetFile+" 没有找到");
            //文件没有找到
            return -1;
        }

        RandomAccessFile sourceFile = new RandomAccessFile(source, "r");
        long nFileLen = sourceFile.length();  //求文件长度

        try {
            OutputStream fos = new FileOutputStream(destination);
            BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER_SIZE);

            bos.write(getHeader(pHeader).a);

            long nLeftLen = nFileLen;
            int OffsetValue = ENCFILE_HEAD_LEN;
            bytearray FileBuf = seal.new bytearray(BUFFER_SIZE);
            byte[] bytes = new byte[BUFFER_SIZE];
            while (nLeftLen > 0) {
                int nRead = sourceFile.read(bytes);
                if (nRead <= 0) {
                    break;
                }

                nLeftLen = nLeftLen - nRead;

                for (int i = 0; i < nRead; i++) {
                    FileBuf.a[i] = bytes[i];
                }

                seal.seal_encrypt(sc, FileBuf, nRead, OffsetValue);
                for (int i = 0; i < nRead; i++) {
                    bytes[i] = FileBuf.a[i];
                }

                bos.write(bytes, 0, nRead);
                bos.flush();

                OffsetValue += nRead;
            }


            sourceFile.close();
            bos.close();
            fos.close();

            return 1;
        } catch (Exception e) {
            return -2;
        }
    }

    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\li\\Desktop\\小区图片模板\\3.jpg");
        EncryptFile(file, new File("C:\\Users\\li\\Desktop\\小区图片模板\\3.jpg"));
    }


}
